package ${config.basePackage}.mapper;

import java.util.List;

import org.apache.ibatis.annotations.DeleteProvider;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.UpdateProvider;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

import ${config.basePackage}.entity.${tableinfo.tableName?cap_first};
import ${config.basePackage}.entity.query.${tableinfo.tableName?cap_first}Query;
import ${config.basePackage}.provider.${tableinfo.tableName?cap_first}SqlProvider;


/**
 * <#if tableinfo.tableComment ?? > ${tableinfo.tableComment}  </#if>
 *
 * ${.now?string('yyyy-MM-dd HH:mm:ss')}
 * 
 * @author 工具自动生成       Lv Bu
 *
 */
@Mapper
@Repository
public interface ${tableinfo.tableName?cap_first}Mapper {

	<#if tableinfo.tableType!='view'>
	// 添加
	@InsertProvider(type = ${tableinfo.tableName?cap_first}SqlProvider.class, method = "insert${tableinfo.tableName?cap_first}")
	<#list tableinfo.columns as column><#if tableinfo.idField==column.columnName && column.columnType=='Integer'>@Options(useGeneratedKeys = true, keyColumn = "${column.dbColumnName}", keyProperty = "${tableinfo.tableName}.${column.columnName}")<#break></#if></#list>
	public Integer insert(@Param("${tableinfo.tableName}") ${tableinfo.tableName?cap_first} ${tableinfo.tableName});
	</#if>
	
	<#if tableinfo.idField!=''>
	// 更新全部属性
	@UpdateProvider(type = ${tableinfo.tableName?cap_first}SqlProvider.class, method = "update${tableinfo.tableName?cap_first}ByPK")
	public Integer update(@Param("${tableinfo.tableName}") ${tableinfo.tableName?cap_first} ${tableinfo.tableName});

	// 更新不为null的属性
	@UpdateProvider(type = ${tableinfo.tableName?cap_first}SqlProvider.class, method = "update${tableinfo.tableName?cap_first}NotNullByPK")
	public Integer updateNotNull(@Param("${tableinfo.tableName}") ${tableinfo.tableName?cap_first} ${tableinfo.tableName});

	// 更新不为""的属性
	@UpdateProvider(type = ${tableinfo.tableName?cap_first}SqlProvider.class, method = "update${tableinfo.tableName?cap_first}NotEmptyByPK")
	public Integer updateNotEmpty(@Param("${tableinfo.tableName}") ${tableinfo.tableName?cap_first} ${tableinfo.tableName});

	// 更新不为""并且不为null的属性
	@UpdateProvider(type = ${tableinfo.tableName?cap_first}SqlProvider.class, method = "update${tableinfo.tableName?cap_first}NotBlankByPK")
	public Integer updateNotBlank(@Param("${tableinfo.tableName}") ${tableinfo.tableName?cap_first} ${tableinfo.tableName});

	// 根据主键删
	@DeleteProvider(type = ${tableinfo.tableName?cap_first}SqlProvider.class, method = "delete${tableinfo.tableName?cap_first}ByPk")
	public Integer delete(@Param("id") <#list tableinfo.columns as column><#if tableinfo.idField==column.columnName>${column.columnType}<#break></#if></#list> id);
	
	// 根据主键查找
	@SelectProvider(type = ${tableinfo.tableName?cap_first}SqlProvider.class, method = "select${tableinfo.tableName?cap_first}ById")
	@Results({
		<#list tableinfo.columns as column>
		@Result(column = "${column.dbColumnName}", property = "${column.columnName}")<#if (column_index+1)<tableinfo.columns?size>,</#if>
		</#list>
	})
	public ${tableinfo.tableName?cap_first} select(@Param("id") <#list tableinfo.columns as column><#if tableinfo.idField==column.columnName>${column.columnType}<#break></#if></#list> id);

	// 根据主键查找并指定显示的列
	@SelectProvider(type = ${tableinfo.tableName?cap_first}SqlProvider.class, method = "select${tableinfo.tableName?cap_first}CusColumnById")
	@Results({
		<#list tableinfo.columns as column>
		@Result(column = "${column.dbColumnName}", property = "${column.columnName}")<#if (column_index+1)<tableinfo.columns?size>,</#if>
		</#list>
	})
	public ${tableinfo.tableName?cap_first} selectCusColumn(@Param("columns") String[] columns, @Param("id") <#list tableinfo.columns as column><#if tableinfo.idField==column.columnName>${column.columnType}<#break></#if></#list> id);
	</#if>
	
	<#if tableinfo.tableType!='view'>
	// 批量插入
	@InsertProvider(type = ${tableinfo.tableName?cap_first}SqlProvider.class, method = "insertList${tableinfo.tableName?cap_first}")
	public Integer insertList${tableinfo.tableName?cap_first}(@Param("list") List<${tableinfo.tableName?cap_first}> ${tableinfo.tableName}s);
	
	// 根据条件删除
	@UpdateProvider(type = ${tableinfo.tableName?cap_first}SqlProvider.class, method = "update${tableinfo.tableName?cap_first}ByQuery")
	@ResultType(Integer.class)
	public Integer update${tableinfo.tableName?cap_first}ByQuery(@Param("query")${tableinfo.tableName?cap_first}Query query,@Param("${tableinfo.tableName}")${tableinfo.tableName?cap_first} ${tableinfo.tableName});

	// 更加条件删除数据
	@DeleteProvider(type = ${tableinfo.tableName?cap_first}SqlProvider.class, method = "delete${tableinfo.tableName?cap_first}ByQuery")
	@ResultType(Integer.class)
	public Integer delete${tableinfo.tableName?cap_first}ByQuery(@Param("query")${tableinfo.tableName?cap_first}Query query);
	</#if>
	// 获取总数
	@SelectProvider(type = ${tableinfo.tableName?cap_first}SqlProvider.class, method = "getCount")
	@ResultType(Long.class)
	public Long getCount();

	// 根据条件查询总条数
	@SelectProvider(type = ${tableinfo.tableName?cap_first}SqlProvider.class, method = "getCountByQuery")
	@ResultType(Long.class)
	public Long getCountByQuery(@Param("query")${tableinfo.tableName?cap_first}Query query);

	// 查询数据集合
	@SelectProvider(type = ${tableinfo.tableName?cap_first}SqlProvider.class, method = "getList")
	@Results({
		<#list tableinfo.columns as column>
		@Result(column = "${column.dbColumnName}", property = "${column.columnName}")<#if (column_index+1)<tableinfo.columns?size>,</#if>
		</#list>
	})
	public List<${tableinfo.tableName?cap_first}> getList(@Param("query") ${tableinfo.tableName?cap_first}Query query);

	
	//根据条件查询单条数据
	@SelectProvider(type = ${tableinfo.tableName?cap_first}SqlProvider.class, method = "get${tableinfo.tableName?cap_first}ByQuery")
	@Results({
		<#list tableinfo.columns as column>
		@Result(column = "${column.dbColumnName}", property = "${column.columnName}")<#if (column_index+1)<tableinfo.columns?size>,</#if>
		</#list>
	})
	public ${tableinfo.tableName?cap_first} get${tableinfo.tableName?cap_first}ByQuery(@Param("query") ${tableinfo.tableName?cap_first}Query query);
	
	// 获取唯一值
	@SelectProvider(type = ${tableinfo.tableName?cap_first}SqlProvider.class, method = "getUniqueResult")
	@ResultType(Object.class)
	public Object getUniqueResult(@Param("query")${tableinfo.tableName?cap_first}Query query,@Param("fieldName")String fieldName);
}
